package com.gimi.cloud.bbp.manage.controller.prod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.prod.EcProdCategoryDTO;
import com.gimi.cloud.bbp.manage.service.prod.EcProdCategoryService;
import com.github.pagehelper.PageInfo;

/**
*  品类相关Controller
* @author wbj
* @Description
* @create 2017-12-18
* @version 1.0
**/
@RestController
@RequestMapping(value = "/category")
public class EcProdCategoryController {
    private final EcProdCategoryService ecProdCategoryService;

    @Autowired
    public EcProdCategoryController(EcProdCategoryService ecProdCategoryService) {
        this.ecProdCategoryService = ecProdCategoryService;
    }

    /**
     * 商品品类根据id查询操作
     * @param ecProdCategoryDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/list")
    public Response categoryList(@RequestBody EcProdCategoryDTO ecProdCategoryDTO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Response resp = new Response();
        ecProdCategoryDTO.setCatGrade(3);
        List<EcProdCategoryDTO> prodCategoryList = ecProdCategoryService.queryList(ecProdCategoryDTO,false);
        Map<String,Object> resData = new HashMap<>();
        resData.put("prodCategorys",prodCategoryList);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"查询成功");
        return resp;

    }
    /**
     * 分页查询
     * @param ecProdCategoryDTO 查询参数
     * @param response
     */
    @RequestMapping(value = "/pageList")
    public Response pageList(@RequestBody EcProdCategoryDTO ecProdCategoryDTO, HttpServletResponse response) throws Exception{
        Response resp = new Response();
        List<EcProdCategoryDTO> prodCategoryList = ecProdCategoryService.queryList(ecProdCategoryDTO,true);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,new PageInfo<>(prodCategoryList));
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"查询成功");
        return resp;
    }

    /**
     * 保存或更新品类信息
     * @param request
     * @param response
     * @param ecProdCategoryDTO 待保存或更新的品类数据
     * @throws Exception
     */
    @RequestMapping(value = "/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@RequestBody EcProdCategoryDTO ecProdCategoryDTO) throws Exception{
        Response resp = new Response();
        ecProdCategoryService.saveOrUpdate(ecProdCategoryDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
        return resp;
    }

    /**
     * 商品品类删除操作
     * @param ecProdCategoryDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/delete")
    public Response categoryDelete(@RequestBody EcProdCategoryDTO ecProdCategoryDTO, HttpServletRequest request, HttpServletResponse response) throws Exception{
        Response resp = new Response();
        ecProdCategoryService.delete(ecProdCategoryDTO,ecProdCategoryDTO.getProdCatId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		return resp;
    }

    @RequestMapping(value = "/gotoEditProdCategory")
    public Response gotoEditProdCatModel(@RequestBody EcProdCategoryDTO prodCategoryDTO, HttpServletResponse response) throws Exception{
        Response resp = new Response();
        EcProdCategoryDTO prodCategory= ecProdCategoryService.loadPordCategoryAndInitModelInfo(prodCategoryDTO,prodCategoryDTO.getProdCatId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("prodCategory",prodCategory);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		return resp;
    }

    /**
     * 根据参数查询出所有的对应商品品类列表
     * @param prodCategoryDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/searchCategoryListByGrade")
    public Response searchCategoryListByGrade(@RequestBody EcProdCategoryDTO prodCategoryDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        List<EcProdCategoryDTO> list = ecProdCategoryService.searchCategoryListByGrade(prodCategoryDTO.getCatGrade() - 1);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
		return resp;
    }
}
